public class OpportunityTaskEventController
{
	public list<TaskEventWraper> listWperAddNewTask{get;set;}
	public string opportunityId{get;set;}
	public OpportunityTaskEventController(ApexPages.StandardController stdController) 
	{
 		Opportunity opp=(Opportunity)stdController.getRecord();
 		opportunityId=opp.Id;
 		Set<Id> setIds=new set<Id>();
 		listWperAddNewTask=new list<TaskEventWraper>();
 		setIds.add(opportunityId);
		List<Quote> LstQuote=[select Id from Quote where OpportunityId=:opportunityId];
		for(Quote Quote1:LstQuote)
			{
				setIds.add(Quote1.Id);
			}
		List<Order__c> lstOrder=[select id from Order__c where Opportunity__c=:opportunityId];
		for(Order__c Order:lstOrder)
		{
			setIds.add(Order.Id);
		}
		List<Task> LstTask=[Select ID,OwnerId,WhatId,Owner.Name,What.Type,Subject,Status,what.Name,Appointment_Type__c,ActivityDate From Task where WhatId in:setIds];
		for(Task tsk:LstTask)
			{
				TaskEventWraper taskenentwrp=new TaskEventWraper();
				taskenentwrp.Id=tsk.ID;
				taskenentwrp.Subject=tsk.Subject;
				taskenentwrp.Appointment=tsk.Appointment_Type__c;
				taskenentwrp.Duedate=String.valueOf(tsk.ActivityDate);
				taskenentwrp.ParentId=tsk.WhatId;
				taskenentwrp.OwnerID=tsk.OwnerId;
				taskenentwrp.RelatedTo=tsk.What.Name;
				if(tsk.What.Type=='Order__c')
					taskenentwrp.Objects ='Fulfillment Order';
				else
					taskenentwrp.Objects=tsk.What.Type;
					taskenentwrp.Status=tsk.Status;
					taskenentwrp.AssignedTo=tsk.Owner.Name;
				listWperAddNewTask.add(taskenentwrp);
			}
		List<Event> LstEvent=[Select ID,OwnerId,WhatId,Owner.Name,what.Name,What.Type,Subject,StartDateTime,Appointment_Type__c From Event where WhatId in:setIds];
		for(Event evnt:LstEvent)
		{
			TaskEventWraper taskenentwrp=new TaskEventWraper();
			taskenentwrp.Id=evnt.ID;
			taskenentwrp.Subject=evnt.Subject;
			taskenentwrp.Appointment=evnt.Appointment_Type__c;
			taskenentwrp.Duedate=String.valueOf(evnt.StartDateTime);
			//valueof(evnt.StartDateTime);
			taskenentwrp.ParentId=evnt.WhatId;
			taskenentwrp.RelatedTo=evnt.What.Name;
			if(evnt.What.Type=='Order__c')
			taskenentwrp.Objects ='Fulfillment Order';
			else
			taskenentwrp.Objects=evnt.What.Type;
			//taskenentwrp.Status=tsk.Status;
			taskenentwrp.AssignedTo=evnt.Owner.Name;
			listWperAddNewTask.add(taskenentwrp);
		}
	}
	public String getParam(String name) 
	{
        return ApexPages.currentPage().getParameters().get(name);   
	}
	public PageReference del() 
	{
		try 
        {
        	String delid = getParam('delid');
        	Integer rowNo =Integer.valueOf(getParam('rowNo'));     
        	listWperAddNewTask.remove(rowNo);    
        	if(delid.startsWith('00T'))
        	{
        		Task tsk=[select id from Task where id=:delid];
        		delete tsk;
        	}
        	else
        	{
        		Event evnt=[select id from Event where id=:delid];
        		delete evnt;
        	}            
        } catch (Exception e) {
            ApexPages.addMessages(e);
        }
        return null;
	}
	public PageReference edit()
	{
        String editid = getParam('editid');
        PageReference pageref=new PageReference('/'+editid+'/e');
        pageref.setredirect(true);
        return pageref;
	}
}